package com.chenwut.threadDemo.exercise_4;

import java.util.Random;

/**
 * @author ChenPi
 * @since 2024/09/19 16:52
 */
public class MyThread extends Thread {

    static int money = 100;
    static int count = 3;

    int MIN = 1; // 最小额


    @Override
    public void run() {
        // while (true) {
        synchronized (MyThread.class) {
            if (count == 0) {
                System.out.println(getName() + " 红包已被抢完！");
            } else {
                int price = 0;

                if (count == 1) {
                    // 表示最后一个红包
                    price = money;
                } else {
                    Random r = new Random();
                    int bounds = money - (count - 1) * MIN;
                    price = r.nextInt(bounds);
                    if (price < MIN){
                        price = MIN;
                    }
                }
                money -= price;
                count--;
                System.out.println(getName() + " 红包抢到了：" + price + "  红包余额为： " + money);
            }
        }
    }
}
